﻿#region | Copyright © Siriwat Suananpornpanit 2013 |

	/*
	##########################################################################
		Software Generator Code For Hashtable From Table Name => USERLOG
		Author By : Siriwat Suananpornpanit
		DateTime : 2013/09/12 11:33:45
	##########################################################################
	*/

#endregion

#region Class | Imports |

	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Data;
	using System.Linq;
	using Microsoft.VisualBasic;
	using System.Text;
	using Web.Dal;
	using Web.Prop;

#endregion

namespace Web.Dao
{
	public class UserlogDao
	{

		/* for DataBase Connection */

		private static DBConnectionsEngine dbEngine;

		private static System.Data.SqlClient.SqlConnection Conn;

		private static Hashtable hashtable;

		private static UserlogDao _dal;

		private System.String sql = "";


		public static UserlogDao Init()
		{
			/* for Init */

			dbEngine = new DBConnectionsEngine();

			Conn = new ConnectionsSql().Connection; 

			_dal = new UserlogDao();

			return _dal;
		}

		public List<Prop.Userlog> Select(Prop.Userlog obj)
		{
			List<Prop.Userlog> entities = new List<Prop.Userlog>();
			try
			{
				System.Text.StringBuilder concast = new System.Text.StringBuilder();

				concast.Append(" ");

				hashtable = new Hashtable();

				if (obj != null)
				{

					if (!String.IsNullOrEmpty(obj.UID))
					{
						concast.Append(" AND UID=:UID ");
						hashtable.Add(":UID", obj.UID);
					}
                    /*
					if (Information.IsDate(obj.UPDATE))
					{
						concast.Append(" AND [UPDATE]=:UPDATE ");
						hashtable.Add(":UPDATE", obj.UPDATE);
					}
                    */
					if (!String.IsNullOrEmpty(obj.LASTIP))
					{
						concast.Append(" AND LASTIP=:LASTIP ");
						hashtable.Add(":LASTIP", obj.LASTIP);
					}

				}
                sql = System.String.Format(@" SELECT L.ID ,(
                                    CASE ISNULL((U.FNAME + ' ' + U.LNAME),'')
                                    WHEN '' THEN
                                    'None'
                                    ELSE
                                    (U.FNAME + ' ' + U.LNAME)
                                    END
                                    )AS [UID]
                                    ,L.[UPDATE], L.[EVENT] ,L.LASTIP  FROM USERLOG L
                                    LEFT JOIN USERINFO U ON L.UID = U.UID
                                    ORDER BY L.[UPDATE] DESC");

				sql = sql + concast.ToString();

				entities = dbEngine.ToList<Prop.Userlog>(sql, hashtable, Conn).ToList();

			}
			catch (Exception ex)
			{
				entities = null;
			}
			return entities;
		}


        public List<Prop.Userlog> ReportLoginSelect()
        {
            List<Prop.Userlog> entities = new List<Prop.Userlog>();
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" ");

                hashtable = new Hashtable();

                sql = System.String.Format(@" SELECT L.ID ,(
                                    CASE ISNULL((U.FNAME + ' ' + U.LNAME),'')
                                    WHEN '' THEN
                                    'None'
                                    ELSE
                                    (U.FNAME + ' ' + U.LNAME)
                                    END
                                    )AS [UID]
                                    ,L.[UPDATE], L.[EVENT] ,L.LASTIP  FROM USERLOG L
                                    LEFT JOIN USERINFO U ON L.UID = U.UID
                                    WHERE (L.[EVENT]='Login' OR L.[EVENT]='LogOut')
                                    ORDER BY L.[UPDATE] DESC");

                sql = sql + concast.ToString();

                entities = dbEngine.ToList<Prop.Userlog>(sql, hashtable, Conn).ToList();

            }
            catch (Exception ex)
            {
                entities = null;
            }
            return entities;
        }

        public Boolean Add(Userlog model)
        {
            Boolean result = false;
            try
            {
                sql = System.String.Format(@" INSERT INTO USERLOG(UID ,[UPDATE] ,LASTIP,[EVENT] ) VALUES('{0}' ,'{1}' ,'{2}','{3}' )",
                    model.UID
                     ,
                    model.UPDATE
                     ,
                    model.LASTIP
                    ,
                    model.EVENT
                     );

                dbEngine.ExecuteNonQuery(sql, Conn);

            }
            catch (Exception ex)
            {
                result = true;
            }
            return result;
        }

        public Boolean Update(Userlog model)
        {
            Boolean result = false;
            try
            {
                sql = System.String.Format(@" UPDATE USERLOG
					SET
					[UPDATE]='{2}' ,
					LASTIP='{3}'
                    WHERE  UID='{1}'
					",
                    model.ID
                     ,
                    model.UID
                     ,
                    model.UPDATE
                     ,
                    model.LASTIP
                     );

                dbEngine.ExecuteNonQuery(sql, Conn);

            }
            catch (Exception ex)
            {
                result = true;
            }
            return result;
        }

        public Boolean Delete()
        {
            Boolean result = false;
            try
            {
                sql = System.String.Format(@" DELETE FROM USERLOG WHERE 1=0 ");

                dbEngine.ExecuteNonQuery(sql, Conn);

            }
            catch (Exception ex)
            {
                result = true;
            }
            return result;
        }

	}
}

	/*
	##########################################################################
		Free Software License
	##########################################################################*/